﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Trirand.Web.Mvc;
using OneDollarStore.Areas.Admin.Models;

namespace OneDollarStore.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class ProductController : Controller
    {
        StoreModelEntities storeDB = new StoreModelEntities();

        public ViewResult Index()
        {
            ProductJqGridModel gridModel = new ProductJqGridModel();
            // Set grid's parameters
            SetUpGrid(gridModel.ProductGrid);
            return View(gridModel);
        }

        public JsonResult SearchGridDataRequested()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            ProductJqGridModel gridModel = new ProductJqGridModel();
            var items = (from p in storeDB.Products
                         select new
                         {
                             p.ProductID,
                             p.SerialProduct.SerialProductID,
                             p.Price,
                             p.Date,
                             p.WarrantyTime,
                             p.WarrantyCompany,
                             p.Quantity
                         }).ToList();
            return gridModel.ProductGrid.DataBind(items.AsQueryable());
        }

        public void EditRows()
        {
            StoreModelEntities storeDB = new StoreModelEntities();
            ProductJqGridModel gridModel = new ProductJqGridModel();
            var editData = gridModel.ProductGrid.GetEditData();
            if (gridModel.ProductGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                int SerialProductID = Convert.ToInt32(editData.RowData["SerialProductID"]);
                // Add to Product table
                Product product = new Product();
                product.SerialProduct = storeDB.SerialProducts.First(p => p.SerialProductID == SerialProductID);
                product.Price = Convert.ToDecimal(editData.RowData["Price"]);
                product.Date = Convert.ToDateTime(editData.RowData["Date"]);
                product.WarrantyTime = Convert.ToDouble(editData.RowData["WarrantyTime"]);
                product.WarrantyCompany = editData.RowData["WarrantyCompany"];
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                storeDB.AddToProducts(product);
                storeDB.SaveChanges();
            }
            if (gridModel.ProductGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int ProductID = Convert.ToInt32(editData.RowData["ProductID"]);
                int SerialProductID = Convert.ToInt32(editData.RowData["SerialProductID"]);
                Product product = (from p in storeDB.Products
                                   where p.ProductID == ProductID
                                   select p).First();
                product.SerialProduct = storeDB.SerialProducts.First(p => p.SerialProductID == SerialProductID);
                product.Price = Convert.ToDecimal(editData.RowData["Price"]);
                product.Date = Convert.ToDateTime(editData.RowData["Date"]);
                product.WarrantyTime = Convert.ToDouble(editData.RowData["WarrantyTime"]);
                product.WarrantyCompany = editData.RowData["WarrantyCompany"];
                product.Quantity = Convert.ToInt32(editData.RowData["Quantity"]);
                storeDB.SaveChanges();
            }
            if (gridModel.ProductGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int ProductID = Convert.ToInt32(editData.RowData["ProductID"]);
                Product product = (from p in storeDB.Products
                                 where p.ProductID == ProductID
                                 select p).First();
                storeDB.DeleteObject(product);
                storeDB.SaveChanges();
            }
        }

        private void SetUpGrid(JQGrid productGrid)
        {
            productGrid.ID = "ProductGrid";
            productGrid.DataUrl = Url.Action("SearchGridDataRequested");
            productGrid.EditUrl = Url.Action("EditRows");
            productGrid.ToolBarSettings.ShowAddButton = true;
            productGrid.ToolBarSettings.ShowEditButton = true;
            productGrid.ToolBarSettings.ShowDeleteButton = true;
            productGrid.ToolBarSettings.ShowSearchButton = true;
            productGrid.ToolBarSettings.ShowRefreshButton = true;
            productGrid.AddDialogSettings.CloseAfterAdding = true;
            productGrid.EditDialogSettings.CloseAfterEditing = true;
            // Setup the dropdown values for the serial product editing dropdown
            SetUpSerialProductEditDropDown(productGrid);
        }

        private void SetUpSerialProductEditDropDown(JQGrid productGrid)
        {
            JQGridColumn categoryColumn = productGrid.Columns.Find(c => c.DataField == "SerialProductID");
            // Populate the search dropdown only on initial request, in order to optimize performance
            if (productGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                StoreModelEntities storeDB = new StoreModelEntities();
                var editList = (from p in storeDB.SerialProducts
                                select p);
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (var item in editList)
                {
                    list.Add(new SelectListItem
                    {
                        Text = item.Name,
                        Value = item.SerialProductID.ToString()
                    });
                }
                categoryColumn.EditList = list;
            }
        }
    }
}